package com.example.foobar.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.example.foobar.business.service.DaNewCommerCountService;
import com.example.foobar.business.service.DaUserChangeRecordService;
import com.example.foobar.business.service.ImsPzGuanhuaiTrackService;
import com.example.foobar.dao.service.DaNewCommerCountDaoService;
import com.example.foobar.entity.DaNewCommerCount;
import com.example.foobar.entity.DaUserChangeRecord;
import com.example.foobar.entity.ImsPzGuanhuaiTrack;
import com.example.foobar.param.DaNewCommerCountPageParam;
import com.example.foobar.utils.DateRange;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 新增用户数记录 服务实现类
 *
 * @author 郑英奔
 * @since 2021-05-31
 */
@Slf4j
@Service
public class DaNewCommerCountServiceImpl implements DaNewCommerCountService {

    public static final String  MESSAGE_TYPE_SUBSCRIBE = "subscribe";

    public static final String ACTION_TIPE = "active";

    public static final String  MESSAGE_TYPE_UNSUBSCRIBE = "unsubscribe";

    public static final String  MESSAGE_TYPE_EVENT = "event";

    public static final String  MESSAGE_TYPE_TEXT = "text";

    @Autowired
    private ImsPzGuanhuaiTrackService imsPzGuanhuaiTrackService;

    @Autowired
    private DaUserChangeRecordService daUserChangeRecordService;


    @Autowired
    private DaNewCommerCountDaoService daNewCommerCountDaoService;


    public boolean saveDaNewCommerCount(DaNewCommerCount daNewCommerCount) throws Exception {
        return daNewCommerCountDaoService.save(daNewCommerCount);
    }

    public boolean updateDaNewCommerCount(DaNewCommerCount daNewCommerCount) throws Exception {
        return daNewCommerCountDaoService.updateById(daNewCommerCount);
    }

    public boolean deleteDaNewCommerCount(Long id) throws Exception {
        return daNewCommerCountDaoService.removeById(id);
    }

    @Override
    public boolean saveBatch(List<DaNewCommerCount> daNewCommerCounts){
       return daNewCommerCountDaoService.saveBatch(daNewCommerCounts);
    }

    @Override
    public void remove(QueryWrapper<DaNewCommerCount> wrapper) {
        daNewCommerCountDaoService.remove(wrapper);
    }

    @Override
    public void reloadNewCommerCountDate() throws ParseException {
        LambdaQueryWrapper<DaNewCommerCount> daNewCommerCountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        daNewCommerCountLambdaQueryWrapper.ge(DaNewCommerCount::getDate, DateUtils.parseDate("2021-6-19", "YYYY-MM-dd"));
        List<DaNewCommerCount> list = daNewCommerCountDaoService.list(daNewCommerCountLambdaQueryWrapper);
        list.forEach(item ->{
            Date date = item.getDate();
            Integer uniacid = item.getUniacid();
            LambdaQueryWrapper<DaUserChangeRecord> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DaUserChangeRecord::getUniacid, uniacid);
            wrapper.eq(DaUserChangeRecord::getDate, date);
            wrapper.eq(DaUserChangeRecord::getType,"subscribe");
            int count = daUserChangeRecordService.count(wrapper);
            item.setAmount(count);

            daNewCommerCountDaoService.updateDaNewCommerCount(item);
        });
    }

    @Override
    public Paging<DaNewCommerCount> getDaNewCommerCountPageList(DaNewCommerCountPageParam daNewCommerCountPageParam) throws Exception {
        return daNewCommerCountDaoService.getDaNewCommerCountPageList(daNewCommerCountPageParam);
    }

    @Override
    public void pullYestdayNewCommerCount(int unicaid, DateRange dateRange) {
        Date startDate = dateRange.getStartDate();
        Date endDate = dateRange.getEndDate();

        while(startDate.compareTo(endDate) <= 0 ){

//            LambdaQueryWrapper<DaNewCommerCount> deleteWrapper = new LambdaQueryWrapper<>(DaNewCommerCount.class).eq(DaNewCommerCount::getUniacid, unicaid).eq(DaNewCommerCount::getDate, "2021-5-25");
//            daNewCommerCountDaoService.deleteByWrapper(deleteWrapper);

            int subscribeCount = imsPzGuanhuaiTrackService.getSubscribeCount(String.valueOf(unicaid), startDate);

            int unSubscribeCount = imsPzGuanhuaiTrackService.getUnsubscribeCount(String.valueOf(unicaid), startDate);

            int realSubscribeCount = subscribeCount - unSubscribeCount;

            DaNewCommerCount daNewCommerCount = new DaNewCommerCount(unicaid, startDate,realSubscribeCount);
            daNewCommerCountDaoService.insertIfNotExist(daNewCommerCount);
            startDate = DateUtils.addDays(startDate, 1);
        }
    }

    @Override
    public DaNewCommerCount getById(Long id) {
        return daNewCommerCountDaoService.getById(id);
    }

    @Override
    public int selectNewCommerCount(String uniacid, Date data) throws Exception {

        int subscribeCount = imsPzGuanhuaiTrackService.getSubscribeCount(uniacid, data);

        int unSubscribeCount = imsPzGuanhuaiTrackService.getUnsubscribeCount(uniacid, data);

        return subscribeCount - unSubscribeCount;
    }

    @Override
    public List<DaNewCommerCount> getDaNewCommerByUniacidAndDate(Integer uniacid, Date baseDate) {


        return daNewCommerCountDaoService.getDaNewCommerByUniacidAndDate(uniacid, baseDate);
    }

    @Override
    public boolean updateRemark(int uniacid, String remark) {
        DaNewCommerCount daNewCommerCount = daNewCommerCountDaoService.getById(uniacid);
        daNewCommerCount.setRemark(remark);
        boolean flag = daNewCommerCountDaoService.saveOrUpdate(daNewCommerCount);
        return flag;
    }

}
